package com.hnas.core.app_common.util;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hnas.core.util.StringUtil;
@Component("postConnServer")
public class PostConnServer {
	
	@Value(value="#{component['postUrl.website']}")
	private  String website;
		
	@Value("#{component['appid']}")
	private String appid;//
	
	@Value(value="#{component['token']}")
	private String token;//令牌
	
	@Value(value="#{component['route']}")
	private String route;//路由名称
	
	@Value(value="#{component['key']}")
	private String key;//加密key
	
	@Value(value="#{component['termNo']}")
	private String termNo;//设备编号，唯一
	
	@Autowired
	private InitAppAction initAppAction;
	
	@SuppressWarnings("rawtypes")
	public  Map getRequestData(Object object,String toroute) throws Exception{
		String url= website;
		System.out.println("=============="+url+"+++++++++++++++++++++");
		JSON json = null;
		Map<String,Object> map = new HashMap<String,Object>();
		//InitAppAction initAppAction = new InitAppAction();
		CommonKeyData commonKeyData = new CommonKeyData(appid, token, route, key, termNo);
		PostKey postKey = initAppAction.initApp(url,commonKeyData);
		System.out.println("初始化令牌2 === " + postKey.getToken());
		try {
		 ObjectMapper objectMapper = new ObjectMapper();
		 AESTool aes = new AESTool();
		 SignatureUtil signatureUtil = new SignatureUtil();
		 String token = postKey.getToken();//令牌
		 String key = postKey.getKey();//加密key
		 System.out.println("appid:"+appid);
		 //请求参数
		 DTORequest dtoRequest=new DTORequest();
		 dtoRequest.setAppid(appid);//APP标识
		 long millis = System.currentTimeMillis();
		 dtoRequest.setMillis(millis);//时间戳
		 String version="1.0.0";
		 dtoRequest.setVersion(version);//接口版本
		 dtoRequest.setRoute(toroute);//路由名称
		 dtoRequest.setToken(token);//令牌
		 dtoRequest.setTermNo(termNo);
		 //加密发送数据
		 String sdatajson;
		 sdatajson = objectMapper.writeValueAsString(object);
		//实体转json
		 String sdata="";
		 System.out.println("加密前的数据是:"+sdatajson);
		 System.out.println("密钥key:"+key);
		 if(!sdatajson.equals("")&&sdatajson!=null&&!sdatajson.equals("null")&&!sdatajson.equals(null)&&!key.equals("")){
		 	sdata = aes.encrypt(sdatajson, key);//加密
		 }
		 //发送数据赋值
		 dtoRequest.setSdata(sdata);
		 String lol="";//摘要
		 String sign = signatureUtil.generateSignature(appid, token, lol,millis);//签名
		 dtoRequest.setSign(sign);
		 //请求实体转json
		 String requestJson=objectMapper.writeValueAsString(dtoRequest);//实体转json
		 //pos数据
		 //发送 POST 请求
		  StringBuffer param=new StringBuffer();
		  param.append("dataType="+DataType.JSON);
		  if(StringUtil.isNotEmpty(requestJson)){
			  requestJson = URLEncoder.encode(requestJson,"UTF-8");
		  }
		  param.append("&sendData="+requestJson);
		  System.out.println("请求参数:"+param.toString());
		  //发送数据
		  String requestData = HttpRequest.sendPost(url,param.toString());
	      System.out.println("请求返回:"+requestData);
	      
	      json = JSON.parseObject(requestData);
	     
	      map.put("json", json);
	      map.put("postKey", postKey);
	      

		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		return map;
	}
}
